package spuStoreWeighRecord

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/apps/purchase/auth"
	"insighttracking/common/utils"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetSpuStoreWeighRecordApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetSpuStoreWeighRecordApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetSpuStoreWeighRecordApiLogic {
	hostname, _ := os.Hostname()
	return &GetSpuStoreWeighRecordApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetSpuStoreWeighRecordApiLogic) GetSpuStoreWeighRecordApi(req *types.GetSpuStoreWeighRecordReq) (resp types.GetSpuStoreWeighRecordResp, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return types.GetSpuStoreWeighRecordResp{
			CompleteStoreWeighRecord: make([]types.GetSpuStoreWeighRecord, 0),
			WaitStoreWeighRecord:     make([]types.GetSpuStoreWeighRecord, 0),
		}, err
	}
	fmt.Println(accountInfo.EnterpriseId, req.PurchaseStoreDetailId, req.SpuID)
	recordEntities, err := service.SpuStoreWeighRecordService.GetSpuStoreWeighRecordApi(l.ctx, accountInfo.EnterpriseId, req.PurchaseStoreDetailId, req.SpuID)
	if err != nil || len(recordEntities) == 0 {
		return types.GetSpuStoreWeighRecordResp{
			CompleteStoreWeighRecord: make([]types.GetSpuStoreWeighRecord, 0),
			WaitStoreWeighRecord:     make([]types.GetSpuStoreWeighRecord, 0),
		}, err
	}
	spuStoreWeighRecord := k.GroupBy(recordEntities, func(item *model.SpuStoreWeighRecordEntity) int64 {
		return item.Status
	})
	var completeStoreWeighRecord = make([]types.GetSpuStoreWeighRecord, 0)
	var waitStoreWeighRecord = make([]types.GetSpuStoreWeighRecord, 0)
	for status, value := range spuStoreWeighRecord {
		if status == 1 {
			completeStoreWeighRecord = k.Map(value, func(item *model.SpuStoreWeighRecordEntity, index int) types.GetSpuStoreWeighRecord {
				return types.GetSpuStoreWeighRecord{
					ID:        item.ID,                                 // 主键id
					SpuID:     item.SpuID,                              // 商品主键,关联到spu表主键id
					Weight:    item.Weight.StringFixed(2),              // 称重重量
					CreatedAt: utils.TimestampToString(item.CreatedAt), // 创建时间
				}
			})
		} else {
			waitStoreWeighRecord = k.Map(value, func(item *model.SpuStoreWeighRecordEntity, index int) types.GetSpuStoreWeighRecord {
				return types.GetSpuStoreWeighRecord{
					ID:        item.ID,                                 // 主键id
					SpuID:     item.SpuID,                              // 商品主键,关联到spu表主键id
					Weight:    item.Weight.StringFixed(2),              // 称重重量
					CreatedAt: utils.TimestampToString(item.CreatedAt), // 创建时间
				}
			})
		}
	}
	return types.GetSpuStoreWeighRecordResp{
		CompleteStoreWeighRecord: completeStoreWeighRecord,
		WaitStoreWeighRecord:     waitStoreWeighRecord,
	}, err
}
